package com.manage.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.manage.dto.OrderAndGoodsDto;
import com.manage.service.FlowerLanguageService;
import com.manage.dto.GoodsAndTypeDto;
import com.manage.pojo.FlowerGoods;
import com.manage.mapper.FlowerGoodsMapper;
import com.manage.pojo.FlowerLanguage;
import com.manage.pojo.FlowerTypes;
import com.manage.service.FlowerGoodsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.manage.service.FlowerTypesService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 */
@Service
public class FlowerGoodsServiceImpl extends ServiceImpl<FlowerGoodsMapper, FlowerGoods> implements FlowerGoodsService {

    @Autowired
    private FlowerGoodsMapper flowerGoodsMapper;

    @Autowired
    private FlowerTypesService flowerTypesService;

    @Autowired
    private FlowerLanguageService flowerLanguageService;

    /**
     * 分页查询
     *
     * @param page
     * @return
     */
    @Override
    public Page pageList(Page page,String userId) {
        return page(page, Wrappers.<FlowerGoods>lambdaQuery().eq(FlowerGoods::getUserId, userId));
    }

    /**
     * 商品搜索
     *
     * @param goods_name
     * @return
     */
    @Override
    public List<FlowerGoods> search_list(String goods_name) {
        List<FlowerGoods> list = list(Wrappers.<FlowerGoods>lambdaQuery().like(FlowerGoods::getGoodsName, goods_name));
        list = traversal(list); // 调用图片分割方法
        return list;
    }

    /**
     * 修改商品状态
     *
     * @param goodsId
     * @param goodsStatus
     * @return
     */
    @Override
    public boolean updateByStatus(String goodsId, Integer goodsStatus) {
        boolean flag =  flowerGoodsMapper.updateByStatus(goodsId,goodsStatus);
        return flag;
    }

    /**
     * 查询单个商品和分类
     *
     * @param goodsId
     * @return
     */
    @Override
    public GoodsAndTypeDto select_update(String goodsId) {
        // 查询商品表
        FlowerGoods goods = getOne(Wrappers.<FlowerGoods>lambdaQuery().eq(FlowerGoods::getGoodsId, goodsId));
        // 查询分类表
        FlowerTypes types = flowerTypesService.getOne(Wrappers.<FlowerTypes>lambdaQuery().eq(FlowerTypes::getGoodsId, goodsId));

        FlowerLanguage language = flowerLanguageService.getOne(Wrappers.<FlowerLanguage>lambdaQuery().eq(FlowerLanguage::getGoodsGid, goodsId));
        GoodsAndTypeDto dto = new GoodsAndTypeDto(); // 定义dto
        BeanUtils.copyProperties(goods,dto); // 数据拷贝
        BeanUtils.copyProperties(types,dto); // 数据拷贝
        dto.setGoodsFlowerLanguage(language.getGoodsFlowerLanguage());
        return dto;
    }

    /**
     * 修改商品和分类
     *
     * @param dto
     * @return
     */
    @Override
    public String updateGoodsAndTypes(GoodsAndTypeDto dto) {
        FlowerGoods goods = new FlowerGoods();
        BeanUtils.copyProperties(dto,goods);
        boolean bool = updateById(goods); // 修改商品
        FlowerTypes types = new FlowerTypes();
        BeanUtils.copyProperties(dto,types);
        types.setGoodsId(goods.getGoodsId());
        boolean flag = flowerTypesService.updateId(types);// 修改分类
        FlowerLanguage language = new FlowerLanguage();
        language.setGoodsFlowerLanguage(dto.getGoodsFlowerLanguage());
        language.setGoodsGid(goods.getGoodsId());
        // 修改花语
        flowerLanguageService.update(Wrappers.<FlowerLanguage>lambdaUpdate().eq(FlowerLanguage::getGoodsGid,language.getGoodsGid())
                .set(FlowerLanguage::getGoodsFlowerLanguage,language.getGoodsFlowerLanguage()));
        if (flag && bool){
            return "修改成功！";
        }else {
            return "修改失败！";
        }
    }

    /**
     * 新增商品和商品分类
     *
     * @param dto
     * @return
     */
    @Override
    public boolean setGoodsAndType(GoodsAndTypeDto dto) {
        FlowerGoods goods = new FlowerGoods();
        BeanUtils.copyProperties(dto,goods);
        boolean save = save(goods);
        FlowerTypes types = new FlowerTypes();
        BeanUtils.copyProperties(dto,types);
        types.setGoodsId(goods.getGoodsId());
        boolean type = flowerTypesService.save(types);
        FlowerLanguage language = new FlowerLanguage();
        language.setGoodsFlowerLanguage(dto.getGoodsFlowerLanguage());
        language.setGoodsGid(goods.getGoodsId());
        flowerLanguageService.save(language);
        if (save && type){ // 判断两个是否都成功
            return true;
        }
        return false;
    }

    /**
     * 查询该商户的所有商品
     *
     * @param userId
     * @return
     */
    @Override
    public List<String> select_user_count(String userId) {
        List<String> list = flowerGoodsMapper.select_user_goodsId(userId);
        return list;
    }

    /**
     * 查询商品前5
     *
     * @param userId
     * @return
     */
    @Override
    public List<OrderAndGoodsDto> order_goods(String userId) {
        List<OrderAndGoodsDto> dtos = flowerGoodsMapper.order_goods(userId);
        return dtos;
    }

    /**
     * 查询所有商品
     *
     * @param userId
     * @return
     */
    @Override
    public List<FlowerGoods> goods_list(String userId) {
        List<FlowerGoods> list = list(Wrappers.<FlowerGoods>lambdaQuery().eq(FlowerGoods::getUserId, userId));
        list = traversal(list);
        return list;
    }

    /**
     * 分割图片
     * @param list
     * @return
     */
    public List<FlowerGoods> traversal(List<FlowerGoods> list){
        List<FlowerGoods> flowerGoods = new ArrayList<>();
        for (FlowerGoods goods : list) {
            if (goods.getGoodsImg() != null){
                String img = goods.getGoodsImg();
                String[] strings = img.split(",");
                goods.setGoodsImg(strings[0]);
            }
            flowerGoods.add(goods);
        }
        return flowerGoods;
    }
}